package com.eshop.modules.template.service.impl;

import java.io.IOException;
import java.util.Iterator;

import com.eshop.utils.FileUtil;

import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.template.service.dto.SystemCityDto;

import java.util.LinkedHashMap;
import java.util.List;

import com.github.pagehelper.PageInfo;

import java.util.Map;

import org.springframework.data.domain.Pageable;
import com.eshop.modules.template.service.dto.SystemCityQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.template.service.SystemCityService;
import com.eshop.modules.template.domain.SystemCity;
import com.eshop.modules.template.service.mapper.SystemCityMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = {Exception.class})
public class SystemCityServiceImpl extends BaseServiceImpl<SystemCityMapper, SystemCity> implements SystemCityService {
    private final IGenerator generator;

    public Map<String, Object> queryAll(final SystemCityQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<SystemCity> page = new PageInfo((List) this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class) SystemCityDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    public List<SystemCity> queryAll(final SystemCityQueryCriteria criteria) {
        return (this.baseMapper).selectList(QueryHelpPlus.getPredicate(SystemCity.class, criteria));
    }

    public void download(final List<SystemCityDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final SystemCityDto yxSystemCity : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("城市id", yxSystemCity.getCityId());
            map.put("省市级别", yxSystemCity.getLevel());
            map.put("父级id", yxSystemCity.getParentId());
            map.put("区号", yxSystemCity.getAreaCode());
            map.put("名称", yxSystemCity.getName());
            map.put("合并名称", yxSystemCity.getMergerName());
            map.put("经度", yxSystemCity.getLng());
            map.put("纬度", yxSystemCity.getLat());
            map.put("是否展示", yxSystemCity.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel((List) list, response);
    }

    public SystemCityServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
